#!/usr/bin/env bash


### BEGIN INIT INFO
# Auther :             huanggh
# Date :             2017-03-29
# Example:            ./task-executor.sh {start|stop|restart}
# Description:       starts the project task-executor Process Manager
### END INIT INFO


#当前工程目录
PROJECT_HOME=$(echo `pwd`)

#存放进程文件
PIDFILE=$PROJECT_HOME/project.pid

#启动日志文件
LOGFILE=$PROJECT_HOME/out.log

#java 位置
JAVA_HOME='/usr/local/java'



case $1 in
    start)
        echo "Starting project task-executor ... "

        #判断程序是否已经在跑
        if [ -r $PIDFILE ] ; then
            PID=$(cat $PIDFILE)
            if [ -n "$PID" ] ; then
                echo "failed, the Process has been started(1) "
                exit 1
            fi
        fi

        FINDPID=`ps aux  |grep java| grep "com.tssup.fesco.ApplicationStarter" |awk '{print $2 " " $11}' |awk '{print $1}'`
        if [ -n "$FINDPID" ] ; then
                echo "failed, the Process has been started(2) "
                echo $FINDPID > $PIDFILE
                exit 1
        fi

        #完整的jvm启动参数，将之前定义的变量组合。
        GC_OPTS="-XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+CMSClassUnloadingEnabled -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintGCDetails -Xloggc:garbage-collection.log -XX:+HeapDumpOnOutOfMemoryError -XX:+UseBiasedLocking -XX:+UseFastAccessorMethods -Djava.util.concurrent.ForkJoinPool.common.parallelism=16"
        MEM_OPTS="-d64 -server -Xms1024m -Xmx1024m -Xmn521m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=256m"

        #删除启动日志文件
        rm -rf $LOGFILE

        #使用nohup命令启动
#        echo 'nohup $JAVA_HOME/bin/java ${JAVA_OPTS} -cp "$PROJECT_HOME/lib/*:$PROJECT_HOME/WebRoot/.:$PROJECT_HOME/config/." -Dfile.encoding=UTF-8 -Xdebug -server -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8892 com.tssup.fesco.ApplicationStarter > $LOGFILE 2>&1 &'
        nohup ${JAVA_HOME}/bin/java ${GC_OPTS} ${MEM_OPTS}  -Dspring.profiles.active=test  -cp "$PROJECT_HOME/lib/*:$PROJECT_HOME/WebRoot/.:$PROJECT_HOME/config/:$PROJECT_HOME/configure/" -Dfile.encoding=UTF-8 -Xdebug -server -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8892 com.tssup.fesco.ApplicationStarter > $LOGFILE 2>&1 &

        #进程号保存在 project.pid文件中
        echo $! > $PIDFILE

        #打印系统启动成功
        echo STARTED

        echo "done."
        ;;

    stop)
        echo "Stopping project gate ... "
        if [ -r $PIDFILE ] ; then
            PID=$(cat $PIDFILE)
            if [ ! -n "$PID" ] ; then
                PID=`ps aux  |grep java| grep "com.tssup.fesco.ApplicationStarter" |awk '{print $2 " " $11}' |awk '{print $1}'`
                if [ ! -n "$PID" ] ; then
                   echo "failed, search pid is empty  - project gate is not running ?(1)"
                    exit 1
                fi
            fi
        else
            PID=`ps aux  |grep java| grep "com.tssup.fesco.ApplicationStarter" |awk '{print $2 " " $11}' |awk '{print $1}'`
            if [ ! -n "$PID" ] ; then
                echo "failed, search pid is empty  - project gate is not running ?(2)"
                exit 1
            fi
        fi
        echo "kill -TERM $PID"
        kill -TERM $PID
        rm -rf $PIDFILE

        echo "done."
        ;;

    restart)
        echo "Restarting project gate ... "
        $0 stop
        sleep 3s
        $0 start
        ;;

    *)
        echo ""
        echo ""
        echo "#######################################"
        echo ""
        echo "please input: $0 {start|stop|restart}  "
        echo ""
        echo "#######################################"
        echo ""
        echo ""

        ;;
esac
exit 0
